﻿namespace com.stifel.BranchMgmt
{
    using System.Reflection;
    using System.Web.UI.WebControls;

    using com.stifel.BranchMgmtDao;
    using com.stifel.Extensions.AssemblyExtensions;

    using Microsoft.SharePoint;

    public partial class BranchMgmtViewUserControl
    {
        #region Methods

        void loadLease( int branchId )
        {
            StifelBranchLeaseDao dao = new StifelBranchLeaseDao( SPContext.Current.Web.Url );
            StifelBranchLease value = dao.Get( branchId );
            uiLeaseExpires.Text = value.LeaseExpires.HasValue ? value.LeaseExpires.Value.ToLongDateString( ) : string.Empty;
            loadLeaseDocuments( value );
        }

        /// <summary>
        /// Loads the lease documents with link, date created and author, date last modified and editor, check-in comment if any
        /// </summary>
        /// <param name="value"></param>
        void loadLeaseDocuments( StifelBranchLease value )
        {
            const string TARGETURL = "%target-url";
            const string TITLE = "%title";
            const string CREATED = "%created";
            const string MODIFIED = "%modified";
            const string AUTHOR = "%author";
            const string EDITOR = "%editor";
            const string COMMENTS = "%comments";

            const string DATESTRING = "M/d/yyyy h:mm tt";

            const string TEMPLATE = "BranchMgmtViewUserControl_Lease.htm";

            string templatefqn = Assembly.GetExecutingAssembly( ).GetFullyQualifiedManifestResourceName( TEMPLATE );
            string template = Assembly.GetExecutingAssembly( ).GetManifestResourceAsString( templatefqn );

            foreach ( SPFile o in value.Attachments )
            {
                string s = template
                    .Replace( TARGETURL, o.Url )
                    .Replace( TITLE, o.Title )
                    .Replace( CREATED, o.TimeCreated.ToString( DATESTRING ) )
                    .Replace( MODIFIED, o.TimeLastModified.ToString( DATESTRING ) )
                    .Replace( AUTHOR, o.Author.LoginName )
                    .Replace( EDITOR, o.ModifiedBy.LoginName )
                    .Replace( COMMENTS, o.CheckInComment );

                uiLeaseDocuments.Controls.Add( new Literal
                {
                    Text = s
                } );
            }
        }

        #endregion Methods
    }
}